<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="玖忆;文鹤;博客">
    
    <meta name="author" content="meteor">
    <!-- preconnect -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    
    
    <!--- Seo Part-->
    
    <link rel="canonical" href="https://wait-you.github.io/2023/06/06/kotlin/"/>
    <meta name="robots" content="index,follow">
    <meta name="googlebot" content="index,follow">
    <meta name="revisit-after" content="1 days">
    
        <meta name="description" content="# Kotlin # 变量和函数 # 变量   如果代码要定义一个变量，需要在变量前面声明这个变量的类型，比如   int a &#x3D; 1;String b &#x3D; &quot;杜金亮&quot;;&lt;!--code￼0--&gt;  可以看到在声明变量时，并没有为了限定类型，那么他的类型时怎么确定的呢？  这是由于 Kotlin 的类型推导机制完成的    但是这种类型推导机制并不是万能的，在一些延迟赋值的情况下，Kotli">
<meta property="og:type" content="article">
<meta property="og:title" content="Kotlin">
<meta property="og:url" content="https://wait-you.github.io/2023/06/06/Kotlin/index.html">
<meta property="og:site_name" content="玖忆">
<meta property="og:description" content="# Kotlin # 变量和函数 # 变量   如果代码要定义一个变量，需要在变量前面声明这个变量的类型，比如   int a &#x3D; 1;String b &#x3D; &quot;杜金亮&quot;;&lt;!--code￼0--&gt;  可以看到在声明变量时，并没有为了限定类型，那么他的类型时怎么确定的呢？  这是由于 Kotlin 的类型推导机制完成的    但是这种类型推导机制并不是万能的，在一些延迟赋值的情况下，Kotli">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/epub_37683759_182">
<meta property="article:published_time" content="2023-06-06T00:52:48.000Z">
<meta property="article:modified_time" content="2023-06-05T08:53:07.009Z">
<meta property="article:author" content="meteor">
<meta property="article:tag" content="Kotlin">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://tuchuang.wenhe9.cn/epub_37683759_182">
    
    
    <!--- Icon Part-->
    <link rel="icon" type="image/png" href="/images/wenhe.png" sizes="192x192">
    <link rel="apple-touch-icon" sizes="180x180" href="/images/wenhe.png">
    <meta name="theme-color" content="#f1404b">
    <link rel="shortcut icon" href="/images/wenhe.png">
    <!--- Page Info-->
    
    <title>
        
            Kotlin -
        
        玖忆
    </title>
    
<link rel="stylesheet" href="/css/style.css">

    
<link rel="stylesheet" href="/assets/fonts.css">

    <!--- Font Part-->
    
    
    
    

    <!--- Inject Part-->
    
    <script id="hexo-configurations">
    let Global = window.Global || {};
    Global.hexo_config = {"hostname":"wait-you.github.io","root":"/","language":"zh-CN"};
    Global.theme_config = {"articles":{"style":{"font_size":"16px","line_height":1.5,"image_border_radius":"14px","image_alignment":"center","image_caption":false,"link_icon":true},"word_count":{"enable":true,"count":true,"min2read":true},"author_label":{"enable":true,"auto":false,"list":[]},"code_block":{"copy":true,"style":"mac","font":{"enable":false,"family":null,"url":null}},"toc":{"enable":true,"max_depth":3,"number":false,"expand":true,"init_open":true},"copyright":true,"lazyload":true,"recommendation":{"enable":false,"title":"推荐阅读","limit":3,"placeholder":"http://tuchuang.wenhe9.cn/default-bg.jpg","skip_dirs":[]}},"colors":{"primary":"#f1404b","secondary":null},"global":{"fonts":{"chinese":{"enable":false,"family":null,"url":null},"english":{"enable":false,"family":null,"url":null}},"content_max_width":"1000px","sidebar_width":"210px","hover":{"shadow":true,"scale":false},"scroll_progress":{"bar":true,"percentage":true},"busuanzi_counter":{"enable":true,"site_pv":true,"site_uv":true,"post_pv":true},"pjax":true,"open_graph":true,"google_analytics":{"enable":false,"id":null}},"home_banner":{"enable":true,"style":"static","image":{"light":"http://tuchuang.wenhe9.cn/default-bg.jpg","dark":"http://tuchuang.wenhe9.cn/default-bg.jpg"},"title":"玖忆","subtitle":{"text":["我本微末凡尘、可也心向天空"],"hitokoto":{"enable":false,"api":"https://v1.hitokoto.cn"},"typing_speed":100,"backing_speed":80,"starting_delay":500,"backing_delay":1500,"loop":true,"smart_backspace":true},"text_color":{"light":"#fff","dark":"#d1d1b6"},"text_style":{"title_size":"2.8rem","subtitle_size":"1.5rem","line_height":1.2},"custom_font":{"enable":false,"family":null,"url":null},"social_links":{"enable":true,"links":{"github":"https://gitee.com/du-jinliang","instagram":null,"zhihu":null,"twitter":null,"email":"dujinliang9@163.com"}}},"plugins":{"feed":{"enable":false},"aplayer":{"enable":false,"type":"fixed","audios":[{"name":null,"artist":null,"url":null,"cover":null}]},"mermaid":{"enable":false,"version":"9.3.0"}},"version":"2.1.4","navbar":{"auto_hide":true,"color":{"left":"#f78736","right":"#367df7","transparency":35},"links":{"Home":{"path":"/","icon":"fa-regular fa-house"}},"search":{"enable":false,"preload":true}},"page_templates":{"friends_column":2,"tags_style":"blur"},"home":{"sidebar":{"enable":true,"position":"left","first_item":"menu","announcement":null,"links":null},"article_date_format":"auto","categories":{"enable":true,"limit":3},"tags":{"enable":true,"limit":3}}};
    Global.language_ago = {"second":"%s 秒前","minute":"%s 分钟前","hour":"%s 小时前","day":"%s 天前","week":"%s 周前","month":"%s 个月前","year":"%s 年前"};
    Global.data_config = {"masonry":false};
  </script>
    
    <!--- Fontawesome Part-->
    
<link rel="stylesheet" href="/fontawesome/fontawesome.min.css">

    
<link rel="stylesheet" href="/fontawesome/brands.min.css">

    
<link rel="stylesheet" href="/fontawesome/solid.min.css">

    
<link rel="stylesheet" href="/fontawesome/regular.min.css">

    
    
    
    
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
<div class="progress-bar-container">
    
        <span class="scroll-progress-bar"></span>
    

    
        <span class="pjax-progress-bar"></span>
        <span class="pjax-progress-icon">
            <i class="fa-solid fa-circle-notch fa-spin"></i>
        </span>
    
</div>


<main class="page-container">

    

    <div class="main-content-container">

        <div class="main-content-header">
            <header class="navbar-container">
    
    <div class="navbar-content">
        <div class="left">
            
            <a class="logo-title" href="/">
                
                玖忆
                
            </a>
        </div>

        <div class="right">
            <!-- PC -->
            <div class="desktop">
                <ul class="navbar-list">
                    
                        
                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class="" 
                                    href="/"  >
                                    
                                        
                                            <i class="fa-regular fa-house"></i>
                                        
                                        首页
                                    
                                </a>
                                <!-- Submenu -->
                                
                            </li>
                    
                    
                </ul>
            </div>
            <!-- Mobile -->
            <div class="mobile">
                
                <div class="icon-item navbar-bar">
                    <div class="navbar-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <!-- Mobile drawer -->
    <div class="navbar-drawer">
        <ul class="drawer-navbar-list">
            
                
                    <li class="drawer-navbar-item flex-center">
                        <a class="" 
                        href="/"  >
                             
                                
                                    <i class="fa-regular fa-house"></i>
                                
                                首页
                            
                        </a>
                    </li>
                    <!-- Submenu -->
                    
            

        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="main-content-body">

            

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="post-page-container">
        <div class="article-content-container">

            
            
                <div class="article-title">
                    <h1 class="article-title-regular">Kotlin</h1>
                </div>
            
                
            

            
                <div class="article-header">
                    <div class="avatar">
                        <img src="/images/wenhe.png">
                    </div>
                    <div class="info">
                        <div class="author">
                            <span class="name">meteor</span>
                            
                                <span class="author-label">Lv3</span>
                            
                        </div>
                        <div class="meta-info">
                            <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fa-regular fa-pen-fancy"></i>&nbsp;
        <span class="desktop">2023-06-05 16:52:48</span>
        <span class="mobile">2023-06-05 16:52</span>
        <span class="hover-info">创建</span>
    </span>
    
        <span class="article-date article-meta-item">
            <i class="fa-regular fa-wrench"></i>&nbsp;
            <span class="desktop">2023-06-05 00:53:07</span>
            <span class="mobile">2023-06-05 00:53</span>
            <span class="hover-info">更新</span>
        </span>
    

    
        <span class="article-categories article-meta-item">
            <i class="fa-regular fa-folders"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/categories/Kotlin/">Kotlin</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    
    
        <span class="article-tags article-meta-item">
            <i class="fa-regular fa-tags"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/tags/Kotlin/">Kotlin</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    

    
    
    
    
        <span class="article-pv article-meta-item">
            <i class="fa-regular fa-eye"></i>&nbsp;<span id="busuanzi_value_page_pv"></span>
        </span>
    
</div>

                        </div>
                    </div>
                </div>
            

            <div class="article-content markdown-body">
                <p># Kotlin</p>
<h2 id="变量和函数"><a class="markdownIt-Anchor" href="#变量和函数">#</a> 变量和函数</h2>
<h3 id="变量"><a class="markdownIt-Anchor" href="#变量">#</a> 变量</h3>
<ul>
<li>
<p>如果代码要定义一个变量，需要在变量前面声明这个变量的类型，比如</p>
<ul>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">int</span> a <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token class-name">String</span> b <span class="token operator">=</span> <span class="token string">"杜金亮"</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">0</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
<li>
<p>可以看到在声明变量时，并没有为了限定类型，那么他的类型时怎么确定的呢？</p>
<ul>
<li>这是由于 Kotlin 的类型推导机制完成的</li>
</ul>
</li>
<li>
<p>但是这种类型推导机制并不是万能的，在一些延迟赋值的情况下，Kotlin 就无法自动推导他的类型了<br>
这时候就需要显式的声明变量类型才行，语法如下</p>
</li>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">val</span> a <span class="token operator">:</span> Int <span class="token operator">=</span> <span class="token number">100</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">1</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>语法糖，当一个函数只有一行时，Kotlin 允许我们不必写函数体，可以直接将唯一的一行代码写在函数定义<br>
尾部，中间用等号连接即可</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">fun</span> <span class="token function">largerNum</span><span class="token punctuation">(</span>num1 <span class="token operator">:</span> Int<span class="token punctuation">,</span> num2 <span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token operator">:</span> Int <span class="token punctuation">&#123;</span> </pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token keyword">return</span> <span class="token function">max</span><span class="token punctuation">(</span>param1<span class="token punctuation">,</span> num2<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="4"></td><td><pre></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token operator">===</span><span class="token operator">=</span><span class="token operator">></span></pre></td></tr><tr><td data-num="6"></td><td><pre></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token keyword">fun</span> <span class="token function">largerNum</span><span class="token punctuation">(</span>num1 <span class="token operator">:</span> Int<span class="token punctuation">,</span> num2 <span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token operator">:</span> Int <span class="token operator">=</span> <span class="token function">max</span><span class="token punctuation">(</span>num1<span class="token punctuation">,</span> num2<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">2</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<h2 id="程序的逻辑控制"><a class="markdownIt-Anchor" href="#程序的逻辑控制">#</a> 程序的逻辑控制</h2>
<h3 id="if条件控制"><a class="markdownIt-Anchor" href="#if条件控制">#</a> if 条件控制</h3>
<ul>
<li>
<p>Kotlin 中的 <code>if</code>  语句和 Java 中的 <code>if</code>  语句几乎没有任何区别</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">fun</span> <span class="token function">largerNum</span><span class="token punctuation">(</span>num1 <span class="token operator">:</span> Int<span class="token punctuation">,</span> num2 <span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token operator">:</span> Int<span class="token punctuation">&#123;</span> </pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token keyword">var</span> temp <span class="token operator">=</span> <span class="token number">0</span> </pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token keyword">if</span><span class="token punctuation">(</span>num1 <span class="token operator">></span> num2<span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="4"></td><td><pre>        temp <span class="token operator">=</span> num1 </pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span> </pre></td></tr><tr><td data-num="6"></td><td><pre>        temp <span class="token operator">=</span> num2 </pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token punctuation">&#125;</span> </pre></td></tr><tr><td data-num="8"></td><td><pre>    <span class="token keyword">return</span> temp</pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">3</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>可以看到 temp 是一个多余的变量，故可以再次修改</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">fun</span> <span class="token function">largerNum</span><span class="token punctuation">(</span>num1 <span class="token operator">:</span> Int<span class="token punctuation">,</span> num2 <span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token operator">:</span> Int<span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token keyword">return</span> <span class="token keyword">if</span><span class="token punctuation">(</span>num1 <span class="token operator">></span> num2<span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="3"></td><td><pre>        num1 </pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="5"></td><td><pre>        num2 </pre></td></tr><tr><td data-num="6"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">4</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<p>​</p>
<h3 id="when条件语句"><a class="markdownIt-Anchor" href="#when条件语句">#</a> when 条件语句</h3>
<ul>
<li>
<p><code>when</code>  语句有点类似 Java 中的 <code>switch</code>  语句，但他又远比语句强大得多</p>
<ul>
<li><code>switch</code>  有着种种限制，比如他只能传入整型或短于整型或者字符串变量作为条件，其次每一个 case 条件都要<br>
最后主动加上一个 break, 否则执行完当前 case 之后会一次执行下面的 case</li>
</ul>
</li>
<li>
<p>下面看一个实例</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">fun</span> <span class="token function">getScore</span><span class="token punctuation">(</span>name <span class="token operator">:</span> String<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token keyword">if</span><span class="token punctuation">(</span>name <span class="token operator">==</span> <span class="token string-literal singleline"><span class="token string">"Tom"</span></span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span> </pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token number">86</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token punctuation">&#125;</span><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>name <span class="token operator">==</span> <span class="token string-literal singleline"><span class="token string">"Jim"</span></span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token number">77</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token punctuation">&#125;</span><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>name <span class="token operator">==</span> <span class="token string-literal singleline"><span class="token string">"Jack"</span></span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="6"></td><td><pre>    <span class="token number">100</span></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="8"></td><td><pre>    <span class="token number">0</span></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">5</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>因为和 <code>if</code>  语句一样，也是可以有返回值的，所以仍旧可以使用单行代码函数的语法糖</p>
</li>
<li>
<p>语句允许传入一个任意类型的参数，然后可以在 when 的结构提中定义一系列的条件，格式是</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre>匹配值 <span class="token operator">-></span> <span class="token punctuation">&#123;</span>执行逻辑<span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">6</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
<li>
<p>上述代码中，关键字是类型匹配的核心，它相当于 Java 中的 <code>instanceof</code>  关键字，由于 <code>checkNumber()</code>  函数接受一个 <code>Number</code>  类型的参数，这是 <code>Kotlin</code>  内置的一个抽象类，像 <code>Int、Float、Double</code>  等与数字相关的类都是他的子类，所以这里就可以使用了类型匹配来判断传入的参数到底属于什么类型</p>
</li>
</ul>
</li>
<li>
<p><code>when</code>  语句的基本用法就这些，但其实 <code>when</code>  语句还有一种不带参数的用法</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">fun</span> <span class="token function">getScore</span><span class="token punctuation">(</span>name <span class="token operator">:</span> String<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token keyword">when</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    name <span class="token operator">==</span> <span class="token string-literal singleline"><span class="token string">"Tom"</span></span> <span class="token operator">-></span> <span class="token number">86</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    name <span class="token operator">==</span> <span class="token string-literal singleline"><span class="token string">"Jack"</span></span> <span class="token operator">-></span> <span class="token number">100</span></pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token keyword">else</span> <span class="token operator">-></span> <span class="token number">0</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">7</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<h3 id="循环语句"><a class="markdownIt-Anchor" href="#循环语句">#</a> 循环语句</h3>
<ul>
<li>
<p>熟悉 Java 的人都晓得，Java 中主要有两种循环语句: <code>while</code>  循环和 <code>for</code>  循环。Kotlin 中也提供了 <code>while</code>  和 <code>for</code>  循环，其中 <code>while</code>  循环不论是语法还是使用技巧上都和 Java 一样，故不讨论</p>
</li>
<li>
<p>Kotlin 在 <code>for</code>  循环方面做了很大幅度的修改，Java 中最常用的 <code>for-i</code>  循环在 Kotlin 直接被舍弃，而 Java 中另一种 <code>for-each</code>  循环则被 Kotlin 进行了大幅度的增强，变成了 <code>for-in</code>  循环</p>
</li>
<li>
<p>在此之前，先普及一个区间的概念</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">val</span> range <span class="token operator">=</span> <span class="token number">0</span><span class="token operator">..</span><span class="token number">10</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">8</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>在很多情况下，双端闭区间却不如单端闭区间好用，比如数组的下标是从 0 开始，一个长度为 10 的数组，他的下标区间范围是 0 到 9，因此左闭右开的区间更加常用，Kotlin 中可以使用 <code>until</code>  关键字来创建一个左闭右开的区间</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">val</span> range <span class="token operator">=</span> <span class="token number">0</span> until <span class="token number">10</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">9</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>默认情况下， <code>for-in</code>  循环每次执行循环时会在区间回味内递增 1，相当于 Java  <code>for-i</code>  循环中 <code>i++</code>  的效果，而如果你想要跳过其中的一些元素，可以使用 <code>step</code>  关键字：</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">for</span><span class="token punctuation">(</span>i <span class="token keyword">in</span> <span class="token number">0</span> until <span class="token number">10</span> step <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token function">println</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">10</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
<li>
<p>相当于 <code>[10, 1]</code></p>
</li>
</ul>
</li>
</ul>
<h2 id="面向对象编程"><a class="markdownIt-Anchor" href="#面向对象编程">#</a> 面向对象编程</h2>
<ul>
<li><strong>什么是面向对象编程？</strong>
<ul>
<li>先将实物封装成具体的类，然后将事物所有的属性和能力分别定义成类中的字段和函数，接下来对类进行实例化，再根据具体的变成需求调用类中的字段和方法即可</li>
</ul>
</li>
</ul>
<h3 id="类与对象"><a class="markdownIt-Anchor" href="#类与对象">#</a> 类与对象</h3>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">class</span> Person<span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token keyword">var</span> name <span class="token operator">=</span> <span class="token string-literal singleline"><span class="token string">""</span></span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token keyword">var</span> age <span class="token operator">=</span> <span class="token number">0</span></pre></td></tr><tr><td data-num="4"></td><td><pre>    </pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token keyword">fun</span> <span class="token function">eat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="6"></td><td><pre>        <span class="token function">println</span><span class="token punctuation">(</span>name <span class="token operator">+</span> <span class="token string-literal singleline"><span class="token string">"is eating. He is "</span></span> <span class="token operator">+</span> age <span class="token operator">+</span> <span class="token string-literal singleline"><span class="token string">"years old."</span></span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="9"></td><td><pre></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token keyword">val</span> p <span class="token operator">=</span> <span class="token function">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="11"></td><td><pre></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span></pre></td></tr><tr><td data-num="13"></td><td><pre></pre></td></tr><tr><td data-num="14"></td><td><pre></pre></td></tr><tr><td data-num="15"></td><td><pre><span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="16"></td><td><pre>    <span class="token keyword">val</span> p <span class="token operator">=</span> <span class="token function">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="17"></td><td><pre>    p<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string-literal singleline"><span class="token string">"Jack"</span></span></pre></td></tr><tr><td data-num="18"></td><td><pre>    p<span class="token punctuation">.</span>age <span class="token operator">=</span> <span class="token number">21</span></pre></td></tr><tr><td data-num="19"></td><td><pre>    p<span class="token punctuation">.</span><span class="token function">eat</span><span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="20"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="21"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">11</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
<li>
<p>想要让 Student 类继承 Person 类需要做两件事</p>
<ul>
<li>
<p>第一，使 Person 类可以被继承</p>
<ul>
<li>
<p>在 Kotlin 中，任何一个非抽象类默认都是不可以被继承的，相当于 Java 中给类声明了 <code>final</code>  关键字，之所以这样设计和 <code>val</code>  关键字的原因是差不多的，因为类和变量一样，最好都是不可变的，而一个类允许被继承的话，他无法预知子类会如何实现，因此可能就会存在一些未知的风险。</p>
</li>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">open</span> <span class="token keyword">class</span> Person<span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token operator">..</span><span class="token operator">..</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">12</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
</li>
<li>
<p><strong>为什么继承的 Person 后面有括号？</strong></p>
<ul>
<li>
<p>在 Java 的继承中，我们知道在子类的构造函数中是要调用父类的构造函数的，但是在 Kotlin 中，构造函数分为主构造函数和次构造函数，最常用的是主构造函数</p>
</li>
<li>
<p>主构造函数的特点是没有函数体，直接定义在类名的后面即可</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">class</span> <span class="token function">Student</span><span class="token punctuation">(</span><span class="token keyword">val</span> id <span class="token operator">:</span> String<span class="token punctuation">,</span> <span class="token keyword">val</span> grade <span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token function">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    </pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">13</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
<li>
<p>根据继承特性的规定，子类的构造函数必须调用父类的构造函数，可是主构造函数并没有函数体，我们怎样去调用父类的构造函数呢？你可能会说，在 <code>init</code>  结构体中去调用不就好了。这或许是一种办法，但是在绝大多数场景下，我们是不需要编写 <code>init</code>  结构体的。在 Kotlin 中子类的主构造函数调用父类中的哪个构造函数，在继承的时候通过括号指定</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">class</span> <span class="token function">Student</span><span class="token punctuation">(</span><span class="token keyword">val</span> id <span class="token operator">:</span> String<span class="token punctuation">,</span> <span class="token keyword">val</span> grade <span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token function">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    </pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">14</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
<li>
<p>这时子类就会出错，需修改为</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">class</span> <span class="token function">Student</span><span class="token punctuation">(</span><span class="token keyword">val</span> id <span class="token operator">:</span> String<span class="token punctuation">,</span> <span class="token keyword">val</span> grade <span class="token operator">:</span> Int<span class="token punctuation">,</span> name <span class="token operator">:</span>String<span class="token punctuation">,</span> age <span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token function">Person</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> age<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">15</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
</li>
<li>
<p>还有一种特殊的情况：类中只有次构造函数，没有主构造函数，即当一个类没有显式定义主构造函数且定义了次构造函数时，他就是没有主构造函数的</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">class</span> Student <span class="token operator">:</span> Person<span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token keyword">constructor</span><span class="token punctuation">(</span>name <span class="token operator">:</span> String<span class="token punctuation">,</span> age <span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token keyword">super</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> age<span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="3"></td><td><pre>        </pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">16</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
</li>
<li>
<p>当一个类没有任何代码时，可以将大括号省略</p>
</li>
</ul>
</li>
<li>
<p>单例类，某个类在全局只能有一个实例，私有一个静态实例，写一个 get 方法，实例化的时候判断实例是否为空，不为空就返回，为空赋值返回</p>
<ul>
<li>
<p>Kotlin 只需要把 <code>class</code>  换为 <code>object</code>  即可</p>
</li>
<li>
<p>调用类似 Java 的静态方法的调用，实际上 Kotlin 在背后自动创建了一个 Singleton 的实例，并且保证全局只会存在一个 Singleton 实例</p>
</li>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre>Object Singleton<span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token keyword">fun</span> <span class="token function">singleton</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="3"></td><td><pre>        <span class="token function">println</span><span class="token punctuation">(</span><span class="token string-literal singleline"><span class="token string">"singleton is called."</span></span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">17</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
<li>
<p>Set</p>
<ul>
<li></li>
</ul>
  <div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">#不可变集合</span><br><span class="line"><span class="keyword">val</span> <span class="keyword">set</span> = setOf(<span class="string">&quot;Apple&quot;</span>, <span class="string">&quot;Banana&quot;</span>, <span class="string">&quot;Orange&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(fruit <span class="keyword">in</span> <span class="keyword">set</span>)&#123;</span><br><span class="line">    println(fruit)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#可变集合</span><br><span class="line"><span class="keyword">val</span> <span class="keyword">set</span> = mutableListOf(<span class="string">&quot;Apple&quot;</span>, <span class="string">&quot;Banana&quot;</span>, <span class="string">&quot;Orange&quot;</span>)</span><br><span class="line">list.add( <span class="string">&quot;Pear&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(fruit <span class="keyword">in</span> <span class="keyword">set</span>)&#123;</span><br><span class="line">    println(fruit)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
</li>
<li>
<p>Map</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre>#存数据</pre></td></tr><tr><td data-num="2"></td><td><pre>map<span class="token punctuation">[</span><span class="token string-literal singleline"><span class="token string">"Apple"</span></span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span></pre></td></tr><tr><td data-num="3"></td><td><pre>#取数据</pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">val</span> number <span class="token operator">=</span> map<span class="token punctuation">[</span><span class="token string-literal singleline"><span class="token string">"Apple"</span></span><span class="token punctuation">]</span></pre></td></tr><tr><td data-num="5"></td><td><pre></pre></td></tr><tr><td data-num="6"></td><td><pre>#不可变集合</pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token keyword">val</span> map <span class="token operator">=</span> <span class="token function">mapOf</span><span class="token punctuation">(</span><span class="token string-literal singleline"><span class="token string">"Apple"</span></span> <span class="token keyword">to</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string-literal singleline"><span class="token string">"Banana"</span></span> <span class="token keyword">to</span> <span class="token number">2</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="8"></td><td><pre>#可变集合</pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token keyword">val</span> map <span class="token operator">=</span> <span class="token function">mutableMapOf</span><span class="token punctuation">(</span><span class="token string-literal singleline"><span class="token string">"Apple"</span></span> <span class="token keyword">to</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string-literal singleline"><span class="token string">"Banana"</span></span> <span class="token keyword">to</span> <span class="token number">2</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="10"></td><td><pre>map<span class="token punctuation">[</span><span class="token string-literal singleline"><span class="token string">"Pear"</span></span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">3</span></pre></td></tr><tr><td data-num="11"></td><td><pre></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token punctuation">(</span>fruit<span class="token punctuation">,</span> number<span class="token punctuation">)</span> <span class="token keyword">in</span> map<span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="13"></td><td><pre>    <span class="token function">println</span><span class="token punctuation">(</span><span class="token string-literal singleline"><span class="token string">"fruit is"</span></span> <span class="token operator">+</span> fruit <span class="token operator">+</span> <span class="token string-literal singleline"><span class="token string">",number is "</span></span> <span class="token operator">+</span> number<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="14"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="15"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">19</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>集合的函数式 API 实质上就是接受了一个 Lambda 参数</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre>#按给定条件查询最大值</pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token keyword">val</span> maxLengthFruit <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">maxBy</span><span class="token punctuation">&#123;</span> it<span class="token punctuation">.</span>length <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="3"></td><td><pre>#按给定条件将集合中的每个元素映射成另外一个值，最终生成一个新的集合</pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">val</span> newList <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">&#123;</span> it<span class="token punctuation">.</span>toUppercase <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="5"></td><td><pre>#按给定条件过滤，最终生成一个新的集合</pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token keyword">val</span> newList <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">&#123;</span> it<span class="token punctuation">.</span>length <span class="token operator">></span> <span class="token number">4</span><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="7"></td><td><pre>#判断集合中是否至少存一个元素满足条件</pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token keyword">val</span> flag <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">any</span><span class="token punctuation">&#123;</span> it<span class="token punctuation">.</span>length <span class="token operator">></span> <span class="token number">5</span> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="9"></td><td><pre>#判断集合中所有元素是否都满足条件</pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token keyword">val</span> flag <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">all</span><span class="token punctuation">&#123;</span> it<span class="token punctuation">.</span>length <span class="token operator">></span> <span class="token number">5</span> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">20</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>但是需要注意的是，一旦参数可以为空，那么下方的调用则必须判断是否为空，不为空才可以调用方法，否则会报错</p>
</li>
</ul>
</li>
<li>
<p>如果每个方法都要写判断语句的话，会很繁琐，所以 Kotlin 提供了一系列的辅助工具</p>
<ul>
<li>
<p><code>?.</code></p>
<ul>
<li>
<p>就是当对象不为空时正常调用，为空则什么都不做</p>
</li>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">fun</span> <span class="token function">doStudy</span><span class="token punctuation">(</span>study <span class="token operator">:</span> Study<span class="token operator">?</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    study<span class="token operator">?</span><span class="token punctuation">.</span><span class="token function">readBooks</span><span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">21</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p><code>!!</code></p>
<ul>
<li>
<p>就是非空断言，表示你非常确信这里的对象不会为空，但是在使用断言时，最好还是问下自己还有没有更好的方案</p>
</li>
<li>
<p>在这里虽然在 main 方法判空了，但是在调用 toUpperCase () 时还会认为这里存在风险，编译不通过</p>
</li>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">var</span> content <span class="token operator">:</span> String<span class="token operator">?</span> <span class="token operator">=</span> <span class="token string-literal singleline"><span class="token string">"hello"</span></span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token keyword">if</span><span class="token punctuation">(</span>content <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="5"></td><td><pre>        <span class="token function">printUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="6"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="8"></td><td><pre></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token keyword">fun</span> <span class="token function">printUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="10"></td><td><pre>    <span class="token keyword">val</span> upperCase <span class="token operator">=</span> content<span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="11"></td><td><pre>    <span class="token function">println</span><span class="token punctuation">(</span>upperCase<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="13"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">22</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
<li>
<p>调用了 obj 的 let 函数，然后 Lambda 表达式中的代码就会立即执行，并且这个 obj 对象本身还会作为参数传递到 Lambda 表达式中，不过为了防止变量重名，这里将参数改成了 obj2，但实际上他们是一个对象，这就是 let 的作用</p>
</li>
<li>
<p>重新审视下这个代码</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">fun</span> <span class="token function">doStudy</span><span class="token punctuation">(</span>study <span class="token operator">:</span> Study<span class="token operator">?</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    study<span class="token operator">?</span><span class="token punctuation">.</span><span class="token function">readBooks</span><span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    study<span class="token operator">?</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">23</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>可以看到对对象的判断出现了两次，这无疑是冗余的，使用 <code>let</code>  修改</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">fun</span> <span class="token function">doStudy</span><span class="token punctuation">(</span>study <span class="token operator">:</span> Study<span class="token operator">?</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    study<span class="token operator">?</span><span class="token punctuation">.</span><span class="token function">let</span><span class="token punctuation">&#123;</span> stu <span class="token operator">-></span></pre></td></tr><tr><td data-num="3"></td><td><pre>         stu<span class="token punctuation">.</span><span class="token function">readBook</span><span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="4"></td><td><pre>         stu<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">24</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>需要补充的一点是，对于全局变量的判空问题，可以使用 <code>let</code> ，但是使用 <code>if</code>  时仍旧会出错，这是因为全局变量的值随时都有可能被其他线程修改，即使做了判空处理，仍然无法保证 <code>if</code>  与剧中的 study 变量没有空指针风险</p>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="补充"><a class="markdownIt-Anchor" href="#补充">#</a> 补充</h2>
<h3 id="字符串内嵌表达式"><a class="markdownIt-Anchor" href="#字符串内嵌表达式">#</a> 字符串内嵌表达式</h3>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">val</span> name <span class="token operator">=</span> <span class="token string-literal singleline"><span class="token string">"杜金亮"</span></span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token function">println</span><span class="token punctuation">(</span><span class="token string-literal singleline"><span class="token string">"hello world"</span></span> <span class="token operator">+</span> name<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="4"></td><td><pre></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token function">println</span><span class="token punctuation">(</span><span class="token string-literal singleline"><span class="token string">"hello world </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">$&#123;</span><span class="token expression">name</span><span class="token interpolation-punctuation punctuation">&#125;</span></span><span class="token string">"</span></span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="6"></td><td><pre></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token comment">// 当表达式内只有一个变量时，大括号可以省略</span></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token function">println</span><span class="token punctuation">(</span><span class="token string-literal singleline"><span class="token string">"hello world </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">$</span><span class="token expression">name</span></span><span class="token string">"</span></span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">25</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
<h2 id="进阶"><a class="markdownIt-Anchor" href="#进阶">#</a> 进阶</h2>
<h3 id="标准函数"><a class="markdownIt-Anchor" href="#标准函数">#</a> 标准函数</h3>
<h4 id="let见上"><a class="markdownIt-Anchor" href="#let见上">#</a> let (见上)</h4>
<h4 id="with"><a class="markdownIt-Anchor" href="#with">#</a> with</h4>
<ul>
<li>
<p><code>with</code>  函数接受两个参数：第一个参数可以是一个任意类型的对象，第二个参数是一个 Lambda 表达式， <code>with</code>  函数会在 Lambda 表达式中提供一个参数对象的上下文，并使用 Lambda 表达式的最后一行代码作为返回值返回</p>
</li>
<li>
<p>普通代码举例</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">val</span> list <span class="token operator">=</span> listOf<span class="token operator">&lt;</span>String<span class="token operator">></span><span class="token punctuation">(</span><span class="token string-literal singleline"><span class="token string">"Apple"</span></span><span class="token punctuation">,</span> <span class="token string-literal singleline"><span class="token string">"Banada"</span></span><span class="token punctuation">,</span> <span class="token string-literal singleline"><span class="token string">"barrery"</span></span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token keyword">val</span> builder <span class="token operator">=</span> <span class="token function">StringBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="3"></td><td><pre>builder<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string-literal singleline"><span class="token string">"Start eating fruits : \n"</span></span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="4"></td><td><pre>list<span class="token punctuation">.</span><span class="token function">forEach</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    builder<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>it<span class="token operator">+</span><span class="token string-literal singleline"><span class="token string">"\n"</span></span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="7"></td><td><pre>builder<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string-literal singleline"><span class="token string">"Ate All fruit"</span></span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token keyword">val</span> result <span class="token operator">=</span> builder<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token function">println</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">26</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<h4 id="run"><a class="markdownIt-Anchor" href="#run">#</a> run</h4>
<ul>
<li>
<p>和 <code>with</code>  函数类似，只是稍微做了一些语法的修改，首先 <code>run</code>  函数通常不会直接调用，而是要在某个对象的基础上调用；其次 <code>run</code>  函数只接受一个 Lambda 参数，斌企鹅会在 Lambda 表达式中提供调用对象的上下文，其他方面和 <code>with</code>  函数一样，包括也会使用 Lambda 表达式中的最后一行代码作为返回值返回</p>
</li>
<li>
<p>对上面的代码进行修改得到:</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">val</span> result <span class="token operator">=</span> <span class="token function">StringBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">run</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token function">append</span><span class="token punctuation">(</span><span class="token string-literal singleline"><span class="token string">"Start eating fruits:\n"</span></span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    list<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="4"></td><td><pre>        <span class="token function">append</span><span class="token punctuation">(</span>it <span class="token operator">+</span> <span class="token string-literal singleline"><span class="token string">"\n"</span></span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="6"></td><td><pre>    <span class="token function">append</span><span class="token punctuation">(</span><span class="token string-literal singleline"><span class="token string">"Ate All fruits\n"</span></span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token function">println</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">27</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<h4 id="use"><a class="markdownIt-Anchor" href="#use">#</a> use</h4>
<ul>
<li>
<p>保证在 <code>lambda</code>  表达式中的代码全部执行完毕后自动将外层的流关闭，这样就不需要我们再去手动关闭流了</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">private</span> <span class="token keyword">fun</span> <span class="token function">save</span><span class="token punctuation">(</span>inputText <span class="token operator">:</span> String<span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token keyword">try</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="3"></td><td><pre>        <span class="token keyword">val</span> output <span class="token operator">=</span> <span class="token function">openFileOutput</span><span class="token punctuation">(</span><span class="token string-literal singleline"><span class="token string">"data"</span></span><span class="token punctuation">,</span> Context<span class="token punctuation">.</span>MODE_PRIVATE<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="4"></td><td><pre>        <span class="token keyword">val</span> writer <span class="token operator">=</span> <span class="token function">BufferedWriter</span><span class="token punctuation">(</span><span class="token function">OutputStreamWriter</span><span class="token punctuation">(</span>output<span class="token punctuation">)</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="5"></td><td><pre>        writer<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="6"></td><td><pre>            it<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>inputText<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="7"></td><td><pre>        <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="8"></td><td><pre>    <span class="token punctuation">&#125;</span><span class="token keyword">catch</span><span class="token punctuation">(</span>e <span class="token operator">:</span> IOException<span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="9"></td><td><pre>        e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="10"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">28</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<h3 id="定义静态方法"><a class="markdownIt-Anchor" href="#定义静态方法">#</a> 定义静态方法</h3>
<ul>
<li>
<p>在 <code>Kotlin</code>  中极度弱化了静态方法这个概念，因为 <code>Kotlin</code>  提供了比静态方法更好的语法特性：<strong>单例类</strong></p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">object</span> Util<span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token keyword">fun</span> <span class="token function">doAction</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="3"></td><td><pre>        <span class="token function">println</span><span class="token punctuation">(</span><span class="token string-literal singleline"><span class="token string">"doSomething"</span></span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">29</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr><tr><td data-num="7"></td><td><pre></pre></td></tr><tr><td data-num="8"></td><td><pre>`</pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>不过， <code>doAction2</code>  方法其实也并不是静态方法， <code>companion object</code>  这个关键字实际上会在 Util 类的内部创建一个伴生类，而 <code>doAction2</code>  方法就是定义在这个伴生类里面的实例方法，只是 Kotlin 会保证 Util 类始终只会存在一个伴生类对象，因此调用 <code>Util.doAction2()</code>  方法实际上就是调用了 Util 类中伴生对象的 <code>doAction2</code>  方法</p>
</li>
<li>
<p>由此可以看出，Kotlin 确实没有直接定义静态方法的关键字，但是提供了一些语法特性来支持类似静态方法调用的写法，这些语法特性基本可以满足我们平时的开发需求了</p>
</li>
<li>
<p>但是如果，你确确实实想要定义真正的静态方法，Kotlin 仍然提供了两种实现方式：注解和顶层方法：</p>
</li>
<li>
<p>先看注解，前面使用单例类和 <code>companion object</code>  都只是在语法的形式上模仿了静态方法的调用方式，实际上他们都不是真正的静态方法，因此如果你在 Java 代码中以静态方法的形式去调用的话，你会发现这些方法并不存在，而如果我们给单例类或 <code>companion object</code>  中的方法加上 <code>@JvmStatic</code>  注解，那么 Kotlin 编译器就会讲这些方法编译成真正的静态方法：</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">class</span> Util<span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token keyword">fun</span> <span class="token function">doAction</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="3"></td><td><pre>        print<span class="token punctuation">;</span><span class="token function">n</span><span class="token punctuation">(</span><span class="token string-literal singleline"><span class="token string">"doSomething"</span></span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    </pre></td></tr><tr><td data-num="6"></td><td><pre>    </pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token keyword">companion</span> <span class="token keyword">object</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="8"></td><td><pre>        <span class="token annotation builtin">@JvmStatic</span></pre></td></tr><tr><td data-num="9"></td><td><pre>        <span class="token keyword">fun</span> <span class="token function">doAction2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="10"></td><td><pre>            <span class="token function">println</span><span class="token punctuation">(</span><span class="token string-literal singleline"><span class="token string">"do Something"</span></span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="11"></td><td><pre>        <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="12"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="13"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="14"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">30</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<h3 id="对变量延迟初始化"><a class="markdownIt-Anchor" href="#对变量延迟初始化">#</a> 对变量延迟初始化</h3>
<ul>
<li>
<p>当你的类中存在很多全局变量实例，为了保证他们能够满足 <code>Kotlin</code>  的空指针检查语法标准，你不得不做许多的非空判断才可以，即使你非常确定他们不会为空</p>
</li>
<li>
<p>延迟初始化使用的关键字是 <code>lateinit</code>  关键字，他可以告诉 <code>Kotlin</code>  编辑器，我会在晚些时候对这个变量进行初始化，这样就不用一开始的时候将他赋值为 <code>null</code>  了.</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">class</span> MainActivity <span class="token operator">:</span> <span class="token function">AppCompatActivity</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> View<span class="token punctuation">.</span><span class="token function">OnClinkListener</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token keyword">private</span> <span class="token keyword">lateinit</span> <span class="token keyword">var</span> adapter <span class="token operator">:</span> MsgAdapter</pre></td></tr><tr><td data-num="3"></td><td><pre>    </pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token keyword">override</span> <span class="token keyword">fun</span> <span class="token function">onCreate</span><span class="token punctuation">(</span>saveInstance <span class="token operator">:</span> Bundle<span class="token operator">?</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="5"></td><td><pre>        <span class="token operator">..</span><span class="token punctuation">.</span></pre></td></tr><tr><td data-num="6"></td><td><pre>        adapter <span class="token operator">=</span> 	<span class="token function">MsgAdapter</span><span class="token punctuation">(</span>msgList<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="7"></td><td><pre>        <span class="token operator">..</span><span class="token punctuation">.</span></pre></td></tr><tr><td data-num="8"></td><td><pre>    <span class="token punctuation">&#125;</span> </pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">31</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>这里定义了一个 Result 接口，用于表示某个操作的结果， 接口中不用编写任何内容，然后定义了两个类去实现 Result 接口，一个 Success 类表示用于成功时的结果，一个 Fail 类表示用于失败时的结果</p>
</li>
<li>
<p>再定义一个 getResultMsg () 方法，用于获取最终结果的信息</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">fun</span> <span class="token function">getResultMsg</span><span class="token punctuation">(</span>result <span class="token operator">:</span> Result<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token keyword">when</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token keyword">is</span> Success <span class="token operator">-></span> result<span class="token punctuation">.</span>msg</pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token keyword">is</span> Fail <span class="token operator">-></span> result<span class="token punctuation">.</span>msg</pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token keyword">else</span> <span class="token operator">-></span> <span class="token keyword">throw</span> <span class="token function">IllegaArgumentException</span><span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token punctuation">&#125;</span> </pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">32</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>此时，getResultMsg 中的 else 已经不再需要了</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">fun</span> <span class="token function">getResultMsg</span><span class="token punctuation">(</span>result <span class="token operator">:</span> Result<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token keyword">when</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token keyword">is</span> Success <span class="token operator">-></span> result<span class="token punctuation">.</span>msg</pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token keyword">is</span> Fail <span class="token operator">-></span> result<span class="token punctuation">.</span>msg</pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token punctuation">&#125;</span> </pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">33</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>示例</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">fun</span> String<span class="token punctuation">.</span><span class="token function">letterCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> Int<span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token keyword">var</span> count <span class="token operator">=</span> <span class="token number">0</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token keyword">for</span> <span class="token punctuation">(</span>char <span class="token keyword">in</span> <span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="4"></td><td><pre>        <span class="token keyword">if</span><span class="token punctuation">(</span>char<span class="token punctuation">.</span>isLetter<span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="5"></td><td><pre>            count<span class="token operator">++</span></pre></td></tr><tr><td data-num="6"></td><td><pre>        <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="8"></td><td><pre>    <span class="token keyword">return</span> count</pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">34</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>语法糖表达式和实际调用函数对照表</p>
<ul>
<li><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/epub_37683759_182"
                      alt="img"
                ></li>
</ul>
</li>
</ul>
<h3 id="高阶函数"><a class="markdownIt-Anchor" href="#高阶函数">#</a> 高阶函数</h3>
<ul>
<li>
<p>基本规则</p>
<ul>
<li>
<div class="highlight-container" data-rel="Kotlin"><figure class="iseeu highlight kotlin"><figcaption data-lang="kotlin"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token punctuation">(</span>String <span class="token punctuation">,</span> Int<span class="token punctuation">)</span> <span class="token operator">-></span> Unit</pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">35</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<h3 id="内联函数"><a class="markdownIt-Anchor" href="#内联函数">#</a> 内联函数</h3>
<ul>
<li>上述的高阶函数对应到 Java 中是什么样子的呢，其实是高阶函数的位置是一个匿名对象，内部重写了方法，这样的效果，就说明当我们大量使用高阶函数时，会有大量的对象的创建和内存的占用，为了解决这种现象我们可以使用内联函数，关键字是 <code>inline</code></li>
<li>使用了内联函数的话，由 <code>kotlin</code>  编辑器的作用，会将高阶函数在编译时完成对调用地方的替换，则避免了无用对象的创建</li>
<li>当我们参数里使用了一个之多个高阶函数时，内联函数都会在编译时完成调用时代码的替换，但是如果我们想要某一个参数不去替换呢，就可以使用 <code>noinline</code>  关键字，为什么会有这种想法呢，是因为内联函数可以 <code>return</code>  而非内联函数不能 <code>return</code>  但可以局部 <code>return</code>  即 <code>return@printString</code> ，需要注意内联函数的 <code>return</code>  其实是外部的 <code>return</code>  即替换位置的 <code>return</code> ，而局部 <code>return</code>  则是匿名对象函数内部的 <code>return</code></li>
<li>但是当我们在一个内联函数的内部调用 <code>Lambda</code>  匿名类时如果使用内联函数会出现错误，这是因为内联函数允许 <code>return</code>  而匿名类不允许 <code>return</code> ，出现了矛盾，就可以使用 <code>crossinline</code>  关键字，他是一个契约，用于保证在内联函数的 <code>Lambda</code>  表达式中一定不会使用 <code>return</code>  关键字，这样矛盾就不存在了</li>
</ul>

            </div>

            
                <div class="post-copyright-info">
                    <div class="article-copyright-info-container">
    <ul>
        <li><strong>标题:</strong> Kotlin</li>
        <li><strong>作者:</strong> meteor</li>
        <li><strong>创建于:</strong> 2023-06-05 16:52:48</li>
        
            <li>
                <strong>更新于:</strong> 2023-06-05 00:53:07
            </li>
        
        <li>
            <strong>链接:</strong> https://gitee.com/du-jinliang/2023/06/06/Kotlin/
        </li>
        <li>
            <strong>版权声明:</strong> 本文章采用 <a class="license" target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0</a> 进行许可。
        </li>
    </ul>
</div>

                </div>
            

            
                <ul class="post-tags-box">
                    
                        <li class="tag-item">
                            <a href="/tags/Kotlin/">#Kotlin</a>&nbsp;
                        </li>
                    
                </ul>
            

            

            
                <div class="article-nav">
                    
                        <div class="article-prev">
                            <a class="prev"
                            rel="prev"
                            href="/2023/06/06/Kotlin%E5%8D%8F%E7%A8%8B/"
                            >
                                <span class="left arrow-icon flex-center">
                                    <i class="fa-solid fa-chevron-left"></i>
                                </span>
                                <span class="title flex-center">
                                    <span class="post-nav-title-item">Kotlin协程</span>
                                    <span class="post-nav-item">上一篇</span>
                                </span>
                            </a>
                        </div>
                    
                    
                        <div class="article-next">
                            <a class="next"
                            rel="next"
                            href="/2023/06/06/JWT/"
                            >
                                <span class="title flex-center">
                                    <span class="post-nav-title-item">JWT</span>
                                    <span class="post-nav-item">下一篇</span>
                                </span>
                                <span class="right arrow-icon flex-center">
                                    <i class="fa-solid fa-chevron-right"></i>
                                </span>
                            </a>
                        </div>
                    
                </div>
            


            
                <div class="comment-container">
                    <div class="comments-container">
    <div id="comment-anchor"></div>
    <div class="comment-area-title">
        <i class="fa-solid fa-comments"></i>&nbsp;评论
    </div>
    

        
            
    <div id="gitalk-container"></div>
    <script data-pjax
            src="//cdn.jsdelivr.net/npm/gitalk/dist/gitalk.min.js"></script>
    <script data-pjax>

        function loadGitalk() {
            let __gitalk__pathname = decodeURI(location.pathname);
            const __gitalk__pathnameLength = __gitalk__pathname.length;
            const __gitalk__pathnameMaxLength = 50;
            if (__gitalk__pathnameLength > __gitalk__pathnameMaxLength) {
                __gitalk__pathname = __gitalk__pathname.substring(0, __gitalk__pathnameMaxLength - 3) + '...';
            }

            try {
                Gitalk && new Gitalk({
                    clientID: '55bad54a77b7e60ad62d',
                    clientSecret: '1031c81500c6be06e338087cb7b713f2d0201b46',
                    repo: 'wait-you.github.io',
                    owner: 'wait-you',
                    admin: ['wait-you'],
                    id: 'comment',
                    language: 'zh-CN'
                }).render('gitalk-container');

            } catch (e) {
                window.Gitalk = null;
            }
        }

        if ('true') {
            const loadGitalkTimeout = setTimeout(() => {
                loadGitalk();
                clearTimeout(loadGitalkTimeout);
            }, 1000);
        } else {
            window.addEventListener('DOMContentLoaded', loadGitalk);
        }
    </script>



        
    
</div>

                </div>
            
        </div>

        
            <div class="toc-content-container">
                <div class="post-toc-wrap">
    <div class="post-toc">
        <div class="toc-title">此页目录</div>
        <div class="page-title">Kotlin</div>
        <ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%98%E9%87%8F%E5%92%8C%E5%87%BD%E6%95%B0"><span class="nav-text"> 变量和函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%98%E9%87%8F"><span class="nav-text"> 变量</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%A8%8B%E5%BA%8F%E7%9A%84%E9%80%BB%E8%BE%91%E6%8E%A7%E5%88%B6"><span class="nav-text"> 程序的逻辑控制</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#if%E6%9D%A1%E4%BB%B6%E6%8E%A7%E5%88%B6"><span class="nav-text"> if 条件控制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#when%E6%9D%A1%E4%BB%B6%E8%AF%AD%E5%8F%A5"><span class="nav-text"> when 条件语句</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5"><span class="nav-text"> 循环语句</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B"><span class="nav-text"> 面向对象编程</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B1%BB%E4%B8%8E%E5%AF%B9%E8%B1%A1"><span class="nav-text"> 类与对象</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%A1%A5%E5%85%85"><span class="nav-text"> 补充</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%86%85%E5%B5%8C%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-text"> 字符串内嵌表达式</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%BF%9B%E9%98%B6"><span class="nav-text"> 进阶</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A0%87%E5%87%86%E5%87%BD%E6%95%B0"><span class="nav-text"> 标准函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%9A%E4%B9%89%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95"><span class="nav-text"> 定义静态方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AF%B9%E5%8F%98%E9%87%8F%E5%BB%B6%E8%BF%9F%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text"> 对变量延迟初始化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%AB%98%E9%98%B6%E5%87%BD%E6%95%B0"><span class="nav-text"> 高阶函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%86%85%E8%81%94%E5%87%BD%E6%95%B0"><span class="nav-text"> 内联函数</span></a></li></ol></li></ol>

    </div>
</div>
            </div>
        
    </div>
</div>


                

            </div>
            
            

        </div>

        <div class="main-content-footer">
            <footer class="footer">
    <div class="info-container">
        <div class="copyright-info">
            &copy;
            
              <span>2022</span>
              -
            
            2023&nbsp;&nbsp;<i class="fa-solid fa-heart fa-beat" style="--fa-animation-duration: 0.5s; color: #f54545"></i>&nbsp;&nbsp;<a href="/">meteor</a>
        </div>
        
            <script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            <div class="website-count info-item">
                
                    <span id="busuanzi_container_site_uv" class="busuanzi_container_site_uv">
                        访问人数&nbsp;<span id="busuanzi_value_site_uv" class="busuanzi_value_site_uv"></span>
                    </span>
                
                
                    <span id="busuanzi_container_site_pv" class="busuanzi_container_site_pv">
                        总访问量&nbsp;<span id="busuanzi_value_site_pv" class="busuanzi_value_site_pv"></span>
                    </span>
                
            </div>
        
        <div class="theme-info info-item">
            <span class="powered-by-container">由 <?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg version="1.1" id="圖層_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="1rem" height="1rem" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve"><path fill="#0E83CD" d="M256.4,25.8l-200,115.5L56,371.5l199.6,114.7l200-115.5l0.4-230.2L256.4,25.8z M349,354.6l-18.4,10.7l-18.6-11V275H200v79.6l-18.4,10.7l-18.6-11v-197l18.5-10.6l18.5,10.8V237h112v-79.6l18.5-10.6l18.5,10.8V354.6z"/></svg><a target="_blank" href="https://hexo.io">Hexo</a> 驱动</span>
                <br>
            <span class="theme-version-container">主题&nbsp;<a class="theme-version" target="_blank" href="https://github.com/EvanNotFound/hexo-theme-redefine">Redefine v2.1.4</a>
        </div>
        
            <div class="icp-info info-item"><a target="_blank" rel="nofollow" href="
                
                    beian.miit.gov.cn
                
                ">冀ICP备20010108号</a></div>
        
        
        
            <div id="start_div" style="display:none">
                2022/8/17 11:45:14
            </div>
            <div>
                博客已运行 <span class="odometer" id="runtime_days" ></span> 天 <span class="odometer" id="runtime_hours"></span> 小时 <span class="odometer" id="runtime_minutes"></span> 分钟 <span class="odometer" id="runtime_seconds"></span> 秒
            </div>
        
        
        
            <script async data-pjax>
                try {
                    function odometer_init() {
                    const elements = document.querySelectorAll('.odometer');
                    elements.forEach(el => {
                        new Odometer({
                            el,
                            format: '( ddd).dd',
                            duration: 200
                        });
                    });
                    }
                    odometer_init();
                } catch (error) {}
            </script>
        
        
        
    </div>  
</footer>
        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="article-tools-list">
        <!-- TOC aside toggle -->
        
            <li class="right-bottom-tools page-aside-toggle">
                <i class="fa-regular fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
            <li class="go-comment">
                <i class="fa-regular fa-comments"></i>
            </li>
        
    </ul>
</div>

        </div>
    

    <div class="right-side-tools-container">
        <div class="side-tools-container">
    <ul class="hidden-tools-list">
        <li class="right-bottom-tools tool-font-adjust-plus flex-center">
            <i class="fa-regular fa-magnifying-glass-plus"></i>
        </li>

        <li class="right-bottom-tools tool-font-adjust-minus flex-center">
            <i class="fa-regular fa-magnifying-glass-minus"></i>
        </li>

        <li class="right-bottom-tools tool-expand-width flex-center">
            <i class="fa-regular fa-expand"></i>
        </li>

        <li class="right-bottom-tools tool-dark-light-toggle flex-center">
            <i class="fa-regular fa-moon"></i>
        </li>

        <!-- rss -->
        

        

        <li class="right-bottom-tools tool-scroll-to-bottom flex-center">
            <i class="fa-regular fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="visible-tools-list">
        <li class="right-bottom-tools toggle-tools-list flex-center">
            <i class="fa-regular fa-cog fa-spin"></i>
        </li>
        
            <li class="right-bottom-tools tool-scroll-to-top flex-center">
                <i class="arrow-up fas fa-arrow-up"></i>
                <span class="percent"></span>
            </li>
        
        
    </ul>
</div>

    </div>

    <div class="image-viewer-container">
    <img src="">
</div>


    


</main>




<script src="/js/utils.js"></script>

<script src="/js/main.js"></script>

<script src="/js/layouts/navbarShrink.js"></script>

<script src="/js/tools/scrollTopBottom.js"></script>

<script src="/js/tools/lightDarkSwitch.js"></script>





    
<script src="/js/tools/codeBlock.js"></script>




    
<script src="/js/layouts/lazyload.js"></script>




    
<script src="/js/tools/runtime.js"></script>

    
<script src="/js/layouts/odometer.min.js"></script>

    
<link rel="stylesheet" href="/assets/odometer-theme-minimal.css">




  
<script src="/js/libs/Typed.min.js"></script>

  
<script src="/js/plugins/typed.js"></script>







<div class="post-scripts pjax">
    
        
<script src="/js/tools/tocToggle.js"></script>

<script src="/js/libs/anime.min.js"></script>

<script src="/js/layouts/toc.js"></script>

<script src="/js/plugins/tabs.js"></script>

    
</div>


    
<script src="/js/libs/pjax.min.js"></script>

<script>
    window.addEventListener('DOMContentLoaded', () => {
        window.pjax = new Pjax({
            selectors: [
                'head title',
                '.page-container',
                '.pjax',
            ],
            history: true,
            debug: false,
            cacheBust: false,
            timeout: 0,
            analytics: false,
            currentUrlFullReload: false,
            scrollRestoration: false,
            // scrollTo: true,
        });

        document.addEventListener('pjax:send', () => {
            Global.utils.pjaxProgressBarStart();
        });

        document.addEventListener('pjax:complete', () => {
            Global.utils.pjaxProgressBarEnd();
            window.pjax.executeScripts(document.querySelectorAll('script[data-pjax], .pjax script'));
            Global.refresh();
        });
    });
</script>




</body>
</html>
